package com.lanou.bookstore.cart.service;

import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.book.service.BookService;
import com.lanou.bookstore.book.service.BookServiceImpl;
import com.lanou.bookstore.cart.domain.CartItem;

import java.util.Iterator;
import java.util.List;

public class CartServiceImpl implements CartService {

    BookService bs = new BookServiceImpl();
    @Override
    public List<CartItem> addCartList(String count, String bid, List<CartItem> list) {
       //添加到购物车

        Book book = new Book();

        List<Book> bookList = bs.queryByBid(bid);
        for (Book book1 : bookList) {
            book = book1;
        }

        CartItem ci = new CartItem();
        ci.setBook(book);
        ci.setNum(Integer.parseInt(count));
        list.add(ci);

        return list;
    }

    @Override
    public List<CartItem> delCartListOne(String bid, List<CartItem> list) {
        //删除单本

        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            CartItem next = (CartItem)iterator.next();

            if (next.getBook().getBid().equals(bid)){
                iterator.remove();
            }
        }

        return list;
    }

    @Override
    public List<CartItem> delCartListAll(List<CartItem> list) {
     //删除所有
        list.clear();

        return list;
    }
}
